package com.xushi.bean;

import com.xushi.model.Activity;
import org.springframework.util.ObjectUtils;

import java.sql.Timestamp;

public class ActivityStateBean implements java.io.Serializable {
    private int state;
    private int code;
    private String message;
    private String return_url;
    private Activity activity;

    public ActivityStateBean() {
        this.code = 2;
        this.message = "活动不存在";
    }

    public ActivityStateBean(Activity activity, String return_url) {
        this.state = getState(activity).ordinal();
        this.activity = activity;
        if (activity == null) {
            this.code = 2;
            this.message = "活动不存在";
        } else if (activity.getUserTemplate() == null) {
            this.code = 2;
            this.message = "无效的活动";
        } else {
            this.code = 1;
            this.message = "成功获取活动状态";
        }
        this.return_url = return_url;
    }

    public static ActivityState getState(Activity activity) {
        if (State.ACTIVE.getState().equals(activity.getState())) {
            Timestamp currentTime = new Timestamp(System.currentTimeMillis());
            Timestamp startTime = activity.getStartTime();
            Timestamp endTime = activity.getEndTime();
            if (currentTime.before(startTime)) {
                return ActivityState.COMING;
            } else if (currentTime.after(endTime)) {
                return ActivityState.ENDED;
            } else {
                return ActivityState.ONGOING;
            }
        } else if (State.DELETED.getState().equals(activity.getState()) ||
                State.INACTIVE.getState().equals(activity.getState())) {
            return ActivityState.ENDED;
        } else {
            return ActivityState.INVALID;
        }
    }

    public static boolean isActivityEnded(Activity activity) {
        if (ObjectUtils.isEmpty(activity))
            throw new NullPointerException();

        Timestamp currentTime = new Timestamp(System.currentTimeMillis());
        Timestamp endTime = activity.getEndTime();
        if (currentTime.after(endTime)) {
            return true;
        }

        return false;
    }

    public static String getStateMeaning(ActivityState state) {
        String message = "";
        switch (state) {
            case INIT:
                message = "还没有创建活动";
                break;
            case COMING:
                message = "活动已经创建，还没有到开始时间";
                break;
            case ENDED:
                message = "活动已经结束";
                break;
            case ONGOING:
                message = "活动正在进行中";
                break;
            case INVALID:
                message = "无效的活动";
                break;
        }
        return message;
    }

    public String getReturn_url() {
        return return_url;
    }

    public void setReturn_url(String return_url) {
        this.return_url = return_url;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

}
